﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.DynamicData;
using System.Web.UI;
using System.Reflection;
using System.Web.UI.WebControls;
using System.Diagnostics;
using System.ComponentModel;
using System.Drawing;
using Catalyst.ComponentModel.DataAnnotations;

namespace Catalyst.Web.DynamicData
{
    /// <summary>
    /// Displays the content that is defined for the filter in templated data-bound controls, using ASP.NET Dynamic Data features.
    /// </summary>
    [Category("Data")]
    [ToolboxBitmap(typeof(DynamicFilterControl))]
    public class DynamicFilterControl : DynamicControl, IAttributeAccessor
    {
        private IDictionary<string, string> _attributes;

        /// <summary>
        /// Hide Mode from designer, it is irrelevant here
        /// </summary>
        [Browsable(false)]
        private new DataBoundControlMode Mode { get; set; }
        
        /// <summary>
        /// The FilterMode used for this control.  This helps determines the type of FilterUserControl that is rendered.
        /// </summary>
        public FilterControlMode FilterMode { get; set; }

        private IFieldTemplate FilterTemplate { get; set; }

        private void CreateControl()
        {           
            Debug.Assert(this.FilterTemplate == null);
            this.FilterTemplate = (new FilterTemplateFactory()).CreateFieldTemplate(this.Column, this.FilterMode, this.UIHint);
            if (this.FilterTemplate != null)
            {
                this.FilterTemplate.SetHost(this);
                if (this._attributes != null)
                {
                    Type type = this.FilterTemplate.GetType();
                    foreach (KeyValuePair<string, string> pair in this._attributes)
                    {
                        PropertyInfo property = type.GetProperty(pair.Key, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                        if (property != null)
                        {
                            object obj2 = PropertyConverter.ObjectFromString(property.PropertyType, property, pair.Value);
                            property.SetValue(this.FilterTemplate, obj2, null);
                        }
                    }
                }
                

                Control filterTemplate = (Control)this.FilterTemplate;
                string controlIDFromColumnName = GetControlIDFromColumnName(this.Column.Name);
                if (this.FindControl(controlIDFromColumnName) == null)
                {
                    filterTemplate.ID = controlIDFromColumnName;
                }
                this.Controls.Add(filterTemplate);
            }
        }

        internal static string GetControlIDFromColumnName(string columnName)
        {
            return ("__" + columnName);
        }

        protected override void OnInit(EventArgs e)
        {
            var controls = new List<Control>();

           foreach (Control control in this.Controls)
                controls.Add(control);
            
            base.OnInit(e);
            
            this.Controls.Clear();
            foreach (var control in controls)
                this.Controls.Add(control);

            if (!base.DesignMode)
            {
                this.CreateControl();
            }
        }

        string IAttributeAccessor.GetAttribute(string key)
        {
            if (this._attributes == null)
            {
                return string.Empty;
            }
            return this._attributes[key];
        }

        void IAttributeAccessor.SetAttribute(string key, string value)
        {
            if (this._attributes == null)
            {
                this._attributes = new Dictionary<string, string>();
            }
            this._attributes[key] = value;
        }
    }
}
